package cz.techi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoadActivity extends Activity implements OnClickListener {
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.load);
	        
	        Button button = (Button)findViewById(R.id.buttonLoadSchedule);
	        button.setOnClickListener(this);
	 }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
		EditText xnameText = (EditText)findViewById(R.id.xname);
		String xname = xnameText.getText().toString();
		
		
		ScheduleLoader sl = new ScheduleLoader(xname);


		Subject[] subjects = sl.getSubjects();
		
		if (sl.getErrorMessage() == null)
		
		{
			ScheduleDb sd = new ScheduleDb(this);
			sd.deleteAll();
			sd.insertAll(subjects);
			sd.close();
			
			Schedule schedule = Schedule.getInstance();
			schedule.setSubjects(subjects);

        	Intent myIntent = new Intent(this, ScheduleActivity.class);
            startActivityForResult(myIntent, 0);
		}
		else
               
		{
			AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Unable to download");
			alertDialog.setMessage(sl.getErrorMessage());
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			   public void onClick(DialogInterface dialog, int which) {
			      // here you can add functions
			   }
			});
			alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
			alertDialog.show();
		}
   	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
	
	  @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Handle item selection
	    	
	    	return MenuHandler.handle(this, item);
	  }
}